﻿@inject IOptionsSnapshot<SiteOptions> SiteOptions
@inject IBlogPostService BlogPostService
@inject IDistributedCacheHelper CacheHelper

@{
    const string cacheKeyForCategories = "_BannerAndHeadlineBoxPartial";

    async Task<BannerViewModel> GetDataFromDb()
    {
        BannerViewModel vm = new();
        vm.Banners = await BlogPostService.BlogPostBriefListByBanner(int.MaxValue);
        var top7NewBlogPost = await BlogPostService.TopNewBlogPostBriefListAsync(7);
        vm.TouTiao = top7NewBlogPost?.FirstOrDefault();
        vm.New2To7 = top7NewBlogPost?.Skip(1).Take(6).ToList();
        return vm;
    }

    var bannerDatas = await CacheHelper.GetOrCreateAsync(cacheKeyForCategories,
        async e => await GetDataFromDb());
    var bannerPosts = bannerDatas?.Banners?.RandomItems(8);
}

<!-- banner begin-->
<div class="banner">
    <div class="swiper-container">
        <div class="swiper-wrapper">
            @if (bannerPosts is {Count:>0})
            {
                @foreach (var blogPost in bannerPosts)
                {
                    <div class="swiper-slide">
                        <a href="@blogPost.CreationTime.ToString("/yyyy/MM")/@blogPost.Slug">
                            <img src="@blogPost.Cover"><span>@blogPost.Title</span>
                        </a>
                    </div>
                }
            }
        </div>
        <div class="swiper-pagination"></div>
        <div class="swiper-button-next"></div>
        <div class="swiper-button-prev"></div>
    </div>
</div>
<!-- banner end-->
<!-- headline-boc begin-->
<div class="headline-box whitebg">
    <div class="headline-news">
        @if (bannerDatas?.TouTiao != null)
        {
            <h2 class="headline-news-title">
                <span>头条</span><a target="_blank" href="@bannerDatas.TouTiao.CreationTime.ToString("/yyyy/MM")/@bannerDatas.TouTiao.Slug">@bannerDatas.TouTiao.Title</a>
            </h2>
            <p class="headline-news-smalltext">@bannerDatas.TouTiao.Description</p>
        }
        @if (bannerDatas?.New2To7 is { Count: > 0 })
        {
            <ul class="headline-news-list">
                @foreach (var blogPost in bannerDatas.New2To7)
                {
                    <li>
                        <a target="_blank" href="@blogPost.CreationTime.ToString("/yyyy/MM")/@blogPost.Slug">@blogPost.Title</a>
                    </li>
                }
            </ul>
        }
    </div>
</div>
<!-- headline-box end-->